﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using libZPlay;
using System.Threading;
using System.IO;

namespace Listener
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Timer timer { get; set; }

        PlayManager PlayInformation { get; set; }
        
        SongListManager MusicList1{ get; set; }

        public MainWindow()
        {
            InitializeComponent();
            MusicList1 = new SongListManager();
            MusicList.ItemsSource = MusicList1.SongList;            

            PlayInformation = new PlayManager();
            this.DataContext = PlayInformation;
            PlayProgress.SetBinding(ProgressBar.ValueProperty, new Binding("CurProgressPercent") { Source = PlayInformation });
        }

        private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed)
                return;
            Point Pos = e.GetPosition(MusicList);

            HitTestResult HitResult = VisualTreeHelper.HitTest(MusicList, Pos);

            if (HitResult == null)
                return;
            if (HitResult.VisualHit is ScrollViewer)
                return;

            SongInformation MusicItem = MusicList.SelectedItem as SongInformation;
            if (MusicItem == null)
                return;
            
            PlayInformation.PlaySong(MusicItem.MusicPath);
        }

        private void AddMusicFileBtn_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofdlg = new Microsoft.Win32.OpenFileDialog();
            ofdlg.Filter = "MP3(*.mp3)|*.mp3|WAV(*.wav)|*.wav;";
            ofdlg.Multiselect = true;

            if (ofdlg.ShowDialog() == false)
                return;

            foreach (string fileName in ofdlg.FileNames)
            {
                AddMusicFile(fileName);
            }
        }

        private bool AddMusicFile(string strFileFullPath)
        {
        	MusicList1.AddSongFileToList(strFileFullPath);
            return true;
        }

        private void RemoveMusicFileBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void QuitBtn_Click(object sender, RoutedEventArgs e)
        {
        	PlayInformation.StopPlay();

            this.Close();
        }

        private void StopPlayBackBtn_Click(object sender, RoutedEventArgs e)
        {
        	PlayInformation.StopPlay();
        }

        private void PlayBackBtn_Click(object sender, RoutedEventArgs e)
        {
        	SongInformation song =  MusicList.SelectedItem as SongInformation;
        	PlayInformation.PlaySong(song.MusicPath);
        }

        private void PausePlayBtn_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void VolumnSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            //Player.SetPlayerVolume((int)VolumnSlider.Value, (int)VolumnSlider.Value);
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
        	PlayInformation.StopPlay();
        }
    }

    class SongListManager : PropertyChangedBase
    {
        public SongListManager()
        {
            _SongList = new ObservableCollection<SongInformation>();
        }

        private ObservableCollection<SongInformation> _SongList { get; set; }
        public ObservableCollection<SongInformation> SongList
        {
            get { return _SongList; }
            set { _SongList = value;}
        }

        public bool AddSongFileToList(string FilePath)
        {
            if (File.Exists(FilePath) == false)
                return false;
            
            _SongList.Add(new SongInformation(){MusicName= System.IO.Path.GetFileNameWithoutExtension(FilePath), MusicPath = FilePath});
            return true;
        }

        public bool RemoveSongFromList(string FilePath)
        {
            SongInformation song = _SongList.FirstOrDefault(Item => Item.MusicPath == FilePath);
            if (song == null)
                return false;
            _SongList.Remove(song);
            return true;
        }

        public string GetFirstSong()
        {
            if (_SongList.Count == 0)
                return string.Empty;
            return _SongList[0].MusicPath;
        }
    }

    
    
    // UI状态
    class UIState : PropertyChangedBase
    {
    	public UIState()
    	{ PlaybackBtnEnable = false; StopPlaybackBtnEnable = false;}
    	
    	private bool _PlaybackBtnEnable;
    	public bool PlaybackBtnEnable
    	{
    		get {return _PlaybackBtnEnable;}
    		set { _PlaybackBtnEnable = value; InvokePropertyChanged("PlaybackBtnEnable");}
    	}
    	
    	private bool _StopPlaybackBtnEnable;
    	public bool StopPlaybackBtnEnable
    	{
    		get { return _StopPlaybackBtnEnable;}
    		set { _StopPlaybackBtnEnable = value; InvokePropertyChanged("StopPlaybackBtnEnable");}
    	}
    }
}
